<?php
/**
 * [商网通 System] Copyright swtshop.cn
 * QQ: 1170787711
 * Author: gj.name
 * This is NOT a freeware
*/
defined('IN_UQ') or exit('Access Denied');
class index_uqcms extends control
{
    function __construct()
    {
        $this->aid = isset($_SESSION['admin']['aid']);
        parent::__construct();
    }
    function index()
    {
        if (isset($_SESSION['admin']['aid'])) {
            header("Location:?a=login");
        }
        $this->display();
    }
    function login_submit()
    {
        if ($_POST) {
            $username = trim($_POST['username']) ? trim($_POST['username']) : error_json("用户名不正确");
            $password = trim($_POST['password']) ? trim($_POST['password']) : error_json("密码不正确");
            $vcode = trim($_POST['vcode']) ? trim($_POST['vcode']) : error_json("验证码不能为空");
            if (strtolower($_SESSION['vcode']) == strtolower($vcode)) {
                $mdpasswd = uqcms_password_hash($password);
                $row = $this->db->get_one("select * from " . table('admin') . " where username = '{$username}' and password = '{$mdpasswd}' limit 1");
                if ($row) {
                    if ($row['aid']) {
                        if ($row['status'] == '1') {
                            $logarr['aid'] = $row['aid'];
                            $logarr['type'] = '1';
                            $logarr['desc'] = '登录系统！';
                            $logarr['ip'] = get_ip();
                            $logarr['addtime'] = time();
                            $query = $this->db->add(table('admin_log'), $logarr);
                            if ($query) {
                                if ($row['aid'] != '1') {
                                    $prow = $this->db->get_one("select * from " . table('admin_power') . " where id = " . $row['power']);
                                    if ($prow['id']) {
                                        $rows['power_con'] = $prow['content'];
                                    }
                                }
                                $rows['aid'] = $row['aid'];
                                $rows['type'] = $row['type'];
                                $rows['username'] = $row['username'];
                                $rows['power'] = $row['power'];
                                $rows['rank'] = $row['rank'];
                                $rows['status'] = $row['status'];
                                @($_SESSION['admin'] = $rows);
                                right_json();
                            } else {
                                $_SESSION['vcode'] = '';
                                error_json('添加失败');
                            }
                        } else {
                            $_SESSION['vcode'] = '';
                            error_json('该账户已被停用');
                        }
                    } else {
                        $_SESSION['vcode'] = '';
                        error_json('用户名或者密码错误');
                    }
                } else {
                    $_SESSION['vcode'] = '';
                    error_json('连接错误');
                }
            } else {
                $_SESSION['vcode'] = '';
                error_json('验证码错误');
            }
        } else {
            error_json('提交错误');
        }
    }
    public function vcode()
    {
        $code = get_vcode_rand('4');
        uqcms::vcode($code);
        $_SESSION['vcode'] = strtolower($code);
    }
    function login()
    {
        $data_one = $this->db->get_all("select * from " . table('admin_nav') . " where pid = 0 and status = 1 order by px asc");
        $this->assign('data_one', $data_one);
        $p_query = $this->db->query("select * from " . table("admin_nav") . " where pid = 0 and status = 1 order by px asc");
        while ($p_row = $this->db->fetch_array($p_query)) {
            $mp_query = $this->db->query("select * from " . table("admin_nav") . " where pid = " . $p_row['id'] . " and status = 1 order by px asc");

            while ($mp_row = $this->db->fetch_array($mp_query)) {
                $smp_query = $this->db->query("select * from " . table("admin_nav") . " where pid = " . $mp_row['id'] . " and status = 1 order by px asc");
                while ($smp_row = $this->db->fetch_array($smp_query)) {
                    $mp_row['sp_menu'][] = $smp_row;
                }
                $p_row['p_menu'][] = $mp_row;
            }
            $menu[] = $p_row;
        }
        $this->assign('menu', $menu);
        if (is_file(UQCMS_PATH . 'stock.php')) {
            $this->assign('stock_status', '1');
        }
        $this->display();
    }
    function logout()
    {
        unset($_SESSION['admin']['aid']);
        unset($_SESSION['admin']['username']);
        unset($_SESSION['admin']['status']);
        echo '<script>location.href="?";</script>';
        exit;
    }
    function panel()
    {
        $mysql_version = $this->db->get_one("select VERSION();");
        $info['mysql_version'] = @$mysql_version['VERSION()'];
        $total_space = disk_total_space(UQCMS_PATH);
        $free_space = disk_free_space(UQCMS_PATH);
        $info['disk'] = array('use_space' => $total_space - $free_space, 'free_space' => format_bytes($free_space), 'total_space' => $total_space);
        $info['php_version'] = PHP_VERSION;
        $info['get_ip'] = get_ip();
        $info['time'] = date("Y-m-d H:i:s");
        $this->assign('info', $info);
        $install_file = UQCMS_PATH . 'install.php';
        if (file_exists($install_file)) {
            $this->assign('install_status', 1);
        }
        $stime = strtotime(date('Y-m-d'));
        $etime = $stime + 86399;
        $today_where = " where addtime >= " . $stime . " and addtime <=" . $etime;
        $user = $this->db->get_one("select count(uid) as usernum from " . table('member') . $today_where);
        $data['usernum'] = $user['usernum'];
        $goods_num = $this->db->get_one("select count(id) as goods_num from " . table('order_info') . $today_where . " and order_status = 2");
        $data['goods_num'] = $goods_num['goods_num'];
        $shop = $this->db->get_one("select count(id) as spnum from " . table('shop_auth') . " where apply_time >=" . $stime . " and apply_time <=" . $etime);
        $data['spnum'] = $shop['spnum'];
        $shop_withdraw = $this->db->get_one("select count(id) as withdraw from " . table('member_withdraw') . $today_where . " and pay_status = 0");
        $data['withdraw'] = $shop_withdraw['withdraw'];
        $this->assign('count', $data);
        $this->display();
    }
    function dirsize($dir)
    {
        @($dh = opendir($dir));
        $size = 0;
        while ($file = @readdir($dh)) {
            if ($file != "." and $file != "..") {
                $path = $dir . "/" . $file;
                if (is_dir($path)) {
                    $size += dirsize($path);
                } elseif (is_file($path)) {
                    $size += filesize($path);
                }
            }
        }
        @closedir($dh);
        return $size;
    }
    function count()
    {
        $dis_space = disk_free_space(UQCMS_PATH);
        echo format_bytes($dis_space);
        $total_space = disk_total_space(UQCMS_PATH);
        echo format_bytes($total_space);
        $dis_space = dirsize(UQCMS_PATH);
        echo format_bytes($dis_space);
        $this->display();
    }
    public function server()
    {
        $web = cfg('yunapi_web');
        $secret = cfg('yunapi_secret');
        $encrypt = md5(md5(base64_encode($web . $secret)));
        $url = 'http://www.uqcms.com/api/login?action=suggest&web=' . base64_encode($web) . '&encrypt=' . $encrypt;
        header('Location:' . $url);
    }
    function apinews()
    {
        echo curl::get('http://www.uqcms.com/api/news');
    }
}